<?php
namespace Application\Controller;

use Application\Model\Dao\UpdateDao;
use Application\Service\XmlService;
use Zend\Authentication\AuthenticationService;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Application\Service\ImageService;
use Application\Service\UpdateService;

class NoticeBoardController extends LoginAwareController
{

    /**
     * @return JsonModel
     * Get all discussion data.
     */
    public function saveNoticeAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $notice = $request->getPost('notice');
            $targetType = $request->getPost('targetType');
            $targetId = $request->getPost('targetId');
            $photoSet = $this->getUploadedFileXml('attached');
            $loginId = $this->getLoggedinUserId();
            $updateDao =  $this->getServiceLocator()->get('update');
            $updateDao->saveUpdateData($notice, $loginId, $photoSet, null, UpdateDao::$UPDATE_TYPE['NOTICE'], $targetType, $targetId);
            $data = array(
                'result' => 'success'
            );
            return new JsonModel($data);
        }
    }

    public function getNoticeAction(){
        $request = $this->getRequest();
        $lastSeenId = $request->getQuery('lastSeenId');
        $after = $request->getQuery('after') == 'false' ? '' : true;
        $limit = $request->getQuery('limit');
        $updateDao = $this->getServiceLocator()->get('update');

        $result = $updateDao->getUpdateData($lastSeenId, $this->getLoggedinUserId(),
            array_filter(UpdateDao::$UPDATE_TYPE, function ($key){
                return $key === 3;
            }), $limit, $after);


        $updateService = new UpdateService();
        $finalResult = $updateService->processUpdate($result, $this->getLoggedinUserId());
        return new JsonModel($finalResult);
    }
}

?>